package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.po.DigitalSliceCase;
import com.zhijian.medical.entity.po.DigitalSliceCaseCollectLike;
import com.zhijian.medical.entity.po.DigitalSliceCaseDifferentialDiagnosis;
import com.zhijian.medical.entity.po.DigitalSliceCaseFile;
import com.zhijian.medical.entity.vo.request.DigitalSliceCaseSearchVo;
import com.zhijian.medical.entity.vo.request.DigitalSliceCaseUpdateBottomVo;
import com.zhijian.medical.entity.vo.request.DigitalSliceCaseUpdateTopVo;
import com.zhijian.medical.entity.vo.response.DigitalSliceCaseDifferentialDiagnosisVo;
import com.zhijian.medical.entity.vo.response.DigitalSliceCaseFileVo;
import com.zhijian.medical.entity.vo.response.DigitalSliceCaseVo;
import com.zhijian.medical.enums.DigitalSliceCaseCollectLikeEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.DigitalSliceCaseCollectLikeService;
import com.zhijian.medical.service.DigitalSliceCaseDifferentialDiagnosisService;
import com.zhijian.medical.service.DigitalSliceCaseFileService;
import com.zhijian.medical.service.DigitalSliceCaseService;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数字切片-病例 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-12-30 15:12:49
 */
@Slf4j
@Controller
@Api(tags = "数字切片-病例")
public class DigitalSliceCaseController {

    @Autowired
    private DigitalSliceCaseService digitalSliceCaseService;

    @Autowired
    private DigitalSliceCaseFileService digitalSliceCaseFileService;

    @Autowired
    private DigitalSliceCaseCollectLikeService digitalSliceCaseCollectLikeService;

    @Autowired
    private DigitalSliceCaseDifferentialDiagnosisService digitalSliceCaseDifferentialDiagnosisService;

    /**
     * 新增病例
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/digital/slice/case")
    @ApiOperation("新增病例")
    public boolean insertCase(@RequestBody DigitalSliceCase param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return digitalSliceCaseService.insert(param) != null;
    }

    /**
     * 删除病例
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/digital/slice/case/{id}")
    @ApiOperation("删除病例")
    public boolean deleteCase(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        digitalSliceCaseService.deleteById(id);
        return true;
    }

    /**
     * 修改病例列表信息
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/digital/slice/case/{id}")
    @ApiOperation("修改病例列表信息")
    public boolean updateCase(@PathVariable("id") Long id, @RequestBody DigitalSliceCaseUpdateTopVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return digitalSliceCaseService.updateTopInfo(param);
    }

    /**
     * 修改病例底部信息，如临床资料/病史、大体所见、病例诊断
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/digital/slice/case/bottom/{id}")
    @ApiOperation("修改病例底部信息，如临床资料/病史、大体所见、病例诊断")
    public boolean updateBottomCase(@PathVariable("id") Long id, @RequestBody DigitalSliceCaseUpdateBottomVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return digitalSliceCaseService.updateBottomInfo(param);
    }


    /**
     * 根据id查询病例
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/digital/slice/case/{id}")
    @ApiOperation("根据id查询病例")
    public DigitalSliceCaseVo selectCaseById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return digitalSliceCaseService.selectVoById(id);
    }

    /**
     * 分页查询病例
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/digital/slice/case/page")
    @ApiOperation("分页查询病例")
    public Page<DigitalSliceCaseVo> selectCaseByCondition(@RequestBody(required = false) DigitalSliceCaseSearchVo param) {
        if (param == null)
            param = DigitalSliceCaseSearchVo.builder().build();
        param.setLoginUserId(CurrentUserHolder.getUser().getId());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return digitalSliceCaseService.selectPage(param);
    }

    /**
     * 新增or覆盖图片
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/digital/slice/case/file")
    @ApiOperation("新增or覆盖图片")
    public boolean saveOrUpdateFile(@RequestBody DigitalSliceCaseFile param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return digitalSliceCaseFileService.saveOrUpdate(param);
    }

    /**
     * 删除某个图片
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/digital/slice/case/file/{id}")
    @ApiOperation("删除某个图片")
    public boolean deleteFile(@PathVariable("id") Long id) {
        digitalSliceCaseFileService.deleteById(id);
        return true;
    }

    /**
     * 根据类型与病例id查询图片列表
     *
     * @param caseId
     * @param type
     * @return
     */
    @Response
    @GetMapping("/digital/slice/case/file/{caseId}")
    @ApiOperation("根据类型与病例id查询图片列表")
    public List<DigitalSliceCaseFileVo> selectById(@PathVariable("caseId") Long caseId,
                                                   @ApiParam(name = "type", value = "类型") @RequestParam(value = "type") String type) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), caseId);
        return digitalSliceCaseFileService.selectByTypeAndCaseId(type, caseId);
    }

    /**
     * 收藏or点赞
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/digital/slice/case/collect/like")
    @ApiOperation("收藏or点赞")
    public boolean insert(@RequestBody DigitalSliceCaseCollectLike param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        param.setUserId(CurrentUserHolder.getUser().getId());
        return digitalSliceCaseCollectLikeService.insert(param) != null;
    }

    /**
     * 我的收藏分页列表
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/digital/slice/case/collect/page")
    @ApiOperation("我的收藏分页列表")
    public Page<DigitalSliceCaseVo> selectCaseCollectPage(@RequestBody(required = false) DigitalSliceCaseSearchVo param) {
        if (param == null)
            param = DigitalSliceCaseSearchVo.builder().build();
        param.setCollectUserId(CurrentUserHolder.getUser().getId());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return digitalSliceCaseService.selectPage(param);
    }

    /**
     * 获取最新的病例ID
     *
     * @return
     */
    @Response
    @GetMapping("/digital/slice/case/newest/id")
    @ApiOperation("获取最新的病例ID")
    public String createNextCaseId() {
        return digitalSliceCaseService.createNextCaseId();
    }

    /**
     * 取消对一个病例的收藏
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/digital/slice/case/cancel/collect/{id}")
    @ApiOperation("取消对一个病例的收藏")
    public boolean cancelCollect(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        digitalSliceCaseCollectLikeService.deleteByTypeAndCaseIdAndUserId(DigitalSliceCaseCollectLikeEnum.COLLECT.getCode().byteValue(), id, CurrentUserHolder.getUser().getId());
        return true;
    }

    /**
     * 新增某个病例的一条鉴别诊断
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/digital/slice/case/differential/diagnosis")
    @ApiOperation("新增某个病例的一条鉴别诊断")
    public List<DigitalSliceCaseDifferentialDiagnosisVo> insert(@RequestBody DigitalSliceCaseDifferentialDiagnosis param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return digitalSliceCaseDifferentialDiagnosisService.saveAll(param);
    }


}